package br.rui.cartoes.persistencia.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import br.rui.cartoes.util.Utils;

public class Banco {

  private static Banco singleton;
  private Properties props = Utils.getProps();
  private Connection conn = null;
  private String driver = props.getProperty("driver");
  private String url = props.getProperty("url");
  private String user = props.getProperty("user");
  private String senha = props.getProperty("senha");

  private Banco(){
	  
  }
  
  public static synchronized Banco getInstance(){
	  
	  if (singleton==null){
		  singleton = new Banco(); 
	  }
	  
	  return singleton;
	  
  }
  public Connection getConexao() {

    try {
      Class.forName(driver);
      conn = DriverManager.getConnection(url,user,senha);
    }
    catch (ClassNotFoundException exc) {
      System.out.println("Error ao carregar o Driver");

    }
    catch (SQLException ex) {
      System.out.println("Problemas ao abrir a conexao com o banco");
     ex.printStackTrace();
    }
    return conn;
  }

  public void closeConexao(Connection conn, ResultSet rs, PreparedStatement stmt){

    if(rs != null){
      try{
        rs.close();
      }catch(SQLException e){}
    }
    if(stmt != null)
      try{
        stmt.close();
      }catch(SQLException ex){}
    if(conn != null)
      try{
        conn.close();
      }catch(SQLException ex){}
  }
}